package secrpc

import (
	"bytes"
	"dub/define/grpcmodel"
	"dub/utils"
	"golang.org/x/net/context"
	"strconv"
	"strings"
)

type SecRpcMemLabel struct {
	log   *utils.Logger
	dbRpc *utils.RpcProxy
}

//根据条件查找会员标签信息
func (s *SecRpcMemLabel) Find(ctx context.Context, args *grpcmodel.ModelSecRpcMemLabelReqFind) (reply *grpcmodel.ModelSecRpcMemLabelResFind, err error) {
	reply = &grpcmodel.ModelSecRpcMemLabelResFind{}
	for {
		//判断参数是否合法
		if args.GetIsPage() && args.GetPageSize() < 1 {
			reply.Err = 3
			break
		}

		db_args := &grpcmodel.ModelDbRpcMemLabelReqFind{
			IsPage:   args.GetIsPage(),
			PageSize: args.GetPageSize(),
			StartRow: args.GetStartRow(),
		}
		db_memLabel := grpcmodel.NewIDbRpcMemLabelClient(s.dbRpc.Conn())
		s.log.Infof("def_func_sec_memlabel.go Find method db_memLabel.Find db_args info is %+v\n", db_args)
		db_reply, err := db_memLabel.Find(s.dbRpc.Context(), db_args)
		if err != nil {
			s.log.Errorf("def_func_sec_memlabel.go Find method db_memLabel.Find err. %v\n", err)
			break
		}
		s.log.Infof("def_func_sec_memlabel.go Find method call db_memLabel.Find db_reply info is %+v\n", db_reply)

		if db_reply.GetErr() > 0 {
			reply.Err = db_reply.GetErr()
			break
		}

		reply.Total = db_reply.GetTotal()
		reply.Rows = make([]*grpcmodel.ModelSecRpcMemLabel, 0, len(db_reply.GetRows()))
		for _, mem_label := range db_reply.GetRows() {
			reply.Rows = append(reply.Rows, &grpcmodel.ModelSecRpcMemLabel{
				MemLabelId:   mem_label.MemLabelId,
				MemLabelName: mem_label.MemLabelName,
			})
		}
		break
	}
	return
}

func (s *SecRpcMemLabel) labelMaps() map[string]string {
	label_map := make(map[string]string)
	for {
		args := &grpcmodel.ModelDbRpcMemLabelReqFind{}
		db_memLabel := grpcmodel.NewIDbRpcMemLabelClient(s.dbRpc.Conn())
		s.log.Infof("def_func_sec_memlabel.go labelMaps method db_memLabel.Find args info is %+v\n", args)
		reply, err := db_memLabel.Find(s.dbRpc.Context(), args)
		s.log.Infof("def_func_sec_memlabel.go labelMaps method db_memLabel.Find reply info is %+v\n", reply)
		if err != nil || reply.GetErr() > 1 || len(reply.GetRows()) < 1 {
			s.log.Errorf("def_func_sec_memlabel.go labelMaps method s.dbRpc.Call MemLabelRpc.FindMemLabelByConds err. %v\n", err)
			break
		}
		if reply.GetErr() == 1 {
			break
		}

		for _, label := range reply.GetRows() {
			label_map[strconv.Itoa(int(label.MemLabelId))] = label.MemLabelName
		}
		break
	}
	return label_map
}

func (s *SecRpcMemLabel) labelIds2NameStrings(ids string, labelMap map[string]string) string {
	buf := bytes.Buffer{}
	for {
		if ids == "" {
			break
		}
		strs := strings.Split(ids, ",")
		isFirst := true
		for _, str := range strs {
			if str != "" {
				if str_val, ok := labelMap[str]; ok {
					if !isFirst {
						buf.WriteString(",")
					}
					buf.WriteString(str_val)
					isFirst = false
				}
			}
		}
		break
	}
	return buf.String()
}

var d_secRpcMemLabel *SecRpcMemLabel

func NewSecRpcMemLabel(log *utils.Logger, dbRpc *utils.RpcProxy) *SecRpcMemLabel {
	if d_secRpcMemLabel == nil {
		d_secRpcMemLabel = &SecRpcMemLabel{
			log:   log,
			dbRpc: dbRpc,
		}
	}
	return d_secRpcMemLabel
}
